package com.wiz.weixin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wiz.weixin.mapper.StationMapper;
import com.wiz.weixin.mapper.WeixinConfigMapper;
import com.wiz.weixin.service.WeixinConfigService;
import com.wiz.weixin.to.StationTO;
import com.wiz.weixin.to.WeixinConfigTO;

/**
 * 优惠
 * @author xxy
 */
@Service("weixinConfigService")
public class WeixinConfigServiceImpl implements WeixinConfigService {

	@Resource
	private WeixinConfigMapper weixinConfigMapper;
	@Resource
	private StationMapper stationMapper;
	
	/**充值优惠*/
	@Override
	public Map<String, List<WeixinConfigTO>> listSaleInfo(List<String> list){
		return assemblyData(weixinConfigMapper.listSaleInfo(list));
	}
	
	/**积分活动*/
	@Override
	public Map<String, List<WeixinConfigTO>> listPointInfo(List<String> list){
		return assemblyData(weixinConfigMapper.listPointInfo(list));
	}
	
	/**油站介绍*/
	@Override
	public List<WeixinConfigTO> listIntroduction(List<String> list){
		return weixinConfigMapper.listSaleInfo(list);
	}
	
	/**组装数据*/
	public Map<String, List<WeixinConfigTO>> assemblyData(List<WeixinConfigTO> result){
		Map<String, List<WeixinConfigTO>> map = new HashMap<String, List<WeixinConfigTO>>();
		List<WeixinConfigTO> configList = null;
		String stationId = null;
		for (WeixinConfigTO bean : result) {
			stationId = bean.getHqId();
			if(map.containsKey(stationId)){
				configList = map.get(stationId);
			}else{
				configList = new ArrayList<WeixinConfigTO>();
			}
			configList.add(bean);
			map.put(stationId, configList);
		}
		return map;
	}
	
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public WeixinConfigTO getWxConfigByHqId(String stationId){
		StationTO station = stationMapper.getStationById(stationId);
		WeixinConfigTO to = weixinConfigMapper.getWxConfigByHqId(station.getHqId());
		to.setQrCode(station.getImgUrl());
		return to;
	}

}
